$(function(){
	layui.use(['table','form',"layer"], function() {
		var table = layui.table;
		var form = layui.form;
		var layer = layui.layer;
		
		table.render({
			elem : '#batch_study',
			page : true,
			limit:5,
			limits:[5,10,15],
			url : '/selectBacthNameCoursewareName', // 数据接口

		   

			response : {
				statusCode : 200
			// 重新规定成功的状态码为 200，table 组件默认为 0
			},
			parseData : function(res) { // res 即为原始返回的数据
				
				return {
					"count" : res.tol,
					"code" : res.status, // 解析接口状态
					"msg" : res.message, // 解析提示文本
					"data" : res.list
				// 解析数据列表
				};
			},
				done:function(res,curr){
					console.log("res:"+res);
					console.log("curr:"+curr);
					currs=curr;
				},
			
			cols : [ [ // 表头
			{
				field : 'batchName',
				title : '批次名字',  
				fixed : 'left',
				align:'center'
			}, {
				field : 'coursewareName',
				title : '课件名字',
				align:'center'
			}, {
				field : 'operate',
				toolbar : '#barDemo',
				title : '操作',
				align:'center'
			} ] ]
			 
		});
		 table.on('tool(batch_study)', function(obj){	
			 var data = obj.data;
			 console.log("kkkk:"+data);
			 if(obj.event=='study'){
				
		        		var index = layer.open({
		        			title:'视频学习',
		       			  	type: 1,
		       			  	content: '<video width="100%" height="100%" id="video" controls="controls" autobuffer="autobuffer" autoplay="autoplay" loop="loop"></video>',
		       			  	area: ['600px', '380px']
		        		});
		        		 var coursewareId=data.coursewareId;
		        		 var batchId=data.batchId;
		        		 var video = document.getElementById("video");
		        		 var time = video.currentTime;
		        		 
		        		 
						 $.ajax({
								url:'/selectSrc',
								dataType:'json',
								type:'post',
								data:{
									coursewareId:coursewareId	
								},
								success:function(res){
									console.log(res.src);
									
									//点击观看	
									 $.ajax({
											url:'/insertStudy',
											dataType:'json',
											type:'post',
											data:{
												coursewareId:coursewareId,
												batchId:batchId,

											},
											success:function(re){
												$("#video").attr("src",res.src);
												if(re.message="1"){
													var curTime=re.time;
													console.log("time1:"+curTime)
													var video = document.getElementById('video');
				        		            		//获取播放进度，并从该进度处继续播放
				        		            		video.currentTime = curTime;
				        		            		//使用事件监听方式捕捉事件
				        		            		video.addEventListener("timeupdate",function(){
				        		            			var timeDisplay;
				        		            			timeDisplay = Math.floor(video.currentTime);
				        		            			//禁止用户快进
				        		            			if (timeDisplay - curTime > 1) {
				        		            				video.currentTime = curTime;
				        		            			}else{
				        		            				curTime = timeDisplay;
				        		            				//更新数据库视频播放进度
				        		            				record();
				        		            			}
				        		            		},false);
				        		            		
				        		            		function record(){
				        		            			$.ajax({
				        		            				url:"/updateTime",
				        		            				dataType:'json',
				        		            				type:'post',
				        		            				data:{
				        		            					coursewareId:coursewareId,
				        		            					batchId:batchId,
				        			            				time:curTime
				        		            				},success:function(res){
				        		            					$.ajax({
						        		            				url:"/updateState",
						        		            				dataType:'json',
						        		            				type:'post',
						        		            				data:{
						        		            					coursewareId:coursewareId,
						        		            					batchId:batchId,
						        			            				
						        		            				},success:function(res){
						        		            					
						        		            				},error:function(){
						        		            					layer.msg("error record");
						        		            				}
						        		            			})
				        		            				},error:function(){
				        		            					layer.msg("error record");
				        		            				}
				        		            			})
				        		            		}

												}
											},
											error:function(){
												layer.msg("系统错误11");
												
											}
										})	
									
								},
								error:function(){
									layer.msg("系统错误11");
									
								}
							})
					
							
							
							
							
							
							
							
			 }
				
			
			 
			 
		 })
		
		
})

})